//
//  YDMyInfoController.m
//  CRM
//
//  Created by ios on 16/7/22.
//  Copyright © 2016年 YD_iOS. All rights reserved.
//

#import "YDMyInfoController.h"
#import "YDUserInfoController.h"
#import "YDCommonSettingsController.h"

//view
#import "YDMyInfoCell.h"
#import "YDMyHeaderView.h"

//model
#import "YDMyInfoModel.h"

#import "UIImageView+WebCache.h"
#import "AFNetworking.h"

#define kHeaderHeight  255.0f

@interface YDMyInfoController () <UITableViewDataSource,UITableViewDelegate,UINavigationControllerDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) YDMyInfoModel *myInfoModel; //销售顾问信息

@property (nonatomic, strong) YDMyHeaderView *headerView;  //表头

@end

@implementation YDMyInfoController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.navigationBarHidden = YES;
    
    [self createView];

}

- (void)viewWillAppear:(BOOL)animated
{
    if (self.tabBarController.selectedIndex != 2) {
        [self.navigationController setNavigationBarHidden:YES animated:NO];
    }else{
        [self.navigationController setNavigationBarHidden:YES animated:YES];
    }
    [super viewWillAppear:animated];
    
    [self getMemberInfoFromNetwork];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    [super viewWillDisappear:animated];
}

#pragma mark - ====================网络==========================
- (void)getMemberInfoFromNetwork
{
    if (self.myInfoModel) {
        self.headerView.myInfoModel = self.myInfoModel;
    }
    
    NSString *url = [NSString stringWithFormat:@"%@sid=%@", kGetMemberInfoAddress, getNSUser(kAPP_SID)];
    
    [[AFHTTPSessionManager manager] POST:url parameters:@{} progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        if ([[responseObject valueForKey:@"code"] isEqualToString:@"S_OK"]) {
            
            //个人信息
            NSDictionary *memberInfo = responseObject[@"var"];
            self.myInfoModel = [YDMyInfoModel instanceWithDict:memberInfo];
            self.headerView.myInfoModel = self.myInfoModel;
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}

#pragma mark - ====================view==========================
- (void)createView
{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.backgroundColor = kViewControllerBackgroundColor;
    [_tableView registerNib(@"YDMyInfoCell")];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.scrollEnabled = NO;
    [self.view addSubview:_tableView];
    
    //表头
    self.headerView = [YDMyHeaderView instanceMyHeaderView];
    _tableView.tableHeaderView = self.headerView;
}

#pragma mark - tableview  数据源和代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YDMyInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YDMyInfoCell"];
    
    if (indexPath.row == 0) {
        //个人信息
        cell.titleLabel.text = @"个人信息";
        cell.leftImageView.image = [UIImage imageNamed:@"icon_me"];
    } else {
        //设置
        cell.titleLabel.text = @"通用设置";
        cell.leftImageView.image = [UIImage imageNamed:@"icon_set"];
    }

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60.0f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    switch (indexPath.row) {
        case 0:
            [self userInfomation];
            break;
        case 1:
            [self commonSettings];
            break;
        default:
            break;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *sectionHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 10.0f)];
    sectionHeaderView.backgroundColor = kViewControllerBackgroundColor;
    return sectionHeaderView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10.0f;
}


#pragma mark - =====================功能类===========================
#pragma mark 个人信息  界面
- (void)userInfomation {
    
    YDUserInfoController *userInfoController = [[YDUserInfoController alloc] init];
    userInfoController.myInfoModel = self.myInfoModel;
    [self.navigationController pushViewController:userInfoController animated:YES];
}

#pragma mark 通用设置
- (void)commonSettings
{
    YDCommonSettingsController *commonSettingsVC = [[YDCommonSettingsController alloc] init];
    [self.navigationController pushViewController:commonSettingsVC animated:YES];
}


@end
